package com.todo.auth.controller;

import cn.hutool.core.bean.BeanUtil;
import com.todo.admin.common.base.SystemLoginUser;
import com.todo.auth.common.utils.AuthenticationUtils;
import com.todo.auth.controller.request.StaffLoginRequest;
import com.todo.auth.controller.request.UpdateUserPasswordRequest;
import com.todo.auth.controller.response.CaptchaResponse;
import com.todo.auth.controller.response.LoginResponse;
import com.todo.auth.remote.dto.StaffDetailDTO;
import com.todo.auth.remote.dto.StaffInfoDTO;
import com.todo.auth.service.StaffLoginService;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author azhebuxing
 * @date 2025/2/20 00:40
 * @description
 */
@Tag(name = "登录API", description = "登录相关接口")
@RestController
@RequestMapping("/auth")
public class StaffLoginController {

    @Resource
    private StaffLoginService staffLoginService;


    /**
     * 生成验证码
     */
    @Operation(summary = "验证码")
    @GetMapping("/captchaImage")
    public BizResponse<CaptchaResponse> getCaptchaImg() {
        CaptchaResponse captchaResponse = staffLoginService.getCaptchaImg();
        return BizResponse.success(captchaResponse);

    }

    /**
     * 登录方法
     *
     * @param request 登录信息
     * @return 结果
     */
    @Operation(summary = "登录")
    @PostMapping("/login")
    public BizResponse<LoginResponse> login(@RequestBody StaffLoginRequest request) {
        // 生成令牌
        StaffDetailDTO staffDetailDTO = staffLoginService.queryStaffDetailByUserName(request.getUsername());

        if (staffDetailDTO == null) {
            throw new BizException(BizErrorCodeEnum.SUCCESS_ACCOUNT_NOT_EXIST);
        }

        String token = staffLoginService.loginAndGetToken(request);
        SystemLoginUser loginUser = AuthenticationUtils.getSystemLoginUser();
        StaffInfoDTO staffInfoDTO = staffLoginService.queryStaffInfoById(loginUser.getUserId());
        LoginResponse loginResponse = new LoginResponse();
        loginResponse.setToken(token);
        LoginResponse.CurrentLoginUserResponse currentLoginUserResponse = new LoginResponse.CurrentLoginUserResponse();
        currentLoginUserResponse.setRoleKey(loginUser.getRoleInfo().getRoleKey());
        currentLoginUserResponse.setPermissions(loginUser.getRoleInfo().getMenuPermissions());

        LoginResponse.UserInfoResponse userInfoResponse = new LoginResponse.UserInfoResponse();
        BeanUtil.copyProperties(staffInfoDTO, userInfoResponse);
        currentLoginUserResponse.setUserInfo(userInfoResponse);

        loginResponse.setCurrentUser(currentLoginUserResponse);

        return BizResponse.success(loginResponse);
    }

    /**
     * 重置密码
     */
    @Operation(summary = "重置个人密码")
    @PutMapping("/updatePassword")
    public BizResponse<Void> updatePassword(@RequestBody UpdateUserPasswordRequest request) {
        //当前逻辑仍在admin中，后续admin统一调整
        return BizResponse.success();
    }


}
